<?php
//---Start add chapter
//Tim truyen bi loi de fix, total_error > 0
if(isset($_GET['feed_truyen'])){
	
		$val=$_GET['q'];
		$db=new db;
		$db->connect();
		
		$sql="select id,name from anime  WHERE total_error>0 and alias like '%".khongdau($val)."%'";
		
		$db->query($sql);
		
		if($db->num_rows()>0){	
			$dt=$db->fetchAll();	
			foreach($dt as $row){				
					$answer[] = array("id"=>$row['id'],"text"=>$row['name']);		
			}
		}else{ 
		$answer[] = array("id"=>"0","text"=>"Không Tìm Thấy Kết quả");
		}
		
		$db->close();
		
		echo json_encode($answer);
		exit();


}


//Fix chap list bi loi
if(isset($_GET['feed_chuong_list'])){

		$val=$_GET['tid'];
		$sql="select id,ten,thutu,fix from chapterk2 where  manga=".intval($val)." and server='error' order by thutu desc";
	
		$db=new db;
		
		$db->connect();
		$db->query($sql);
		if($db->num_rows()>0){	
			$dt=$db->fetchAll();
			$html=' <select name="chuong" id="chuong" onchange="get_chuong_data(this.value)"  >
						<option value="">Chọn Chapter------</option>
                                          
					
					 ';	
			foreach($dt as $row){				
					$html.= '<option value="'.$row['id'].'">'.$row['thutu'].' - '.$row['ten'];
					if($row['fix']==1){
						$html.=' - Fixed';
						}
					$html.='</option>';		
			}
			$html.=' </select> ';
		}else{ 
				$html=' <select name="chuong" id="chuong"  >
						<option value="">Truyện chưa có chapter nào</option>
                                          
					
					 ';	
		}
		
		$db->close();
		
		echo $html;
		exit();


}

//Fix chap content
if(isset($_GET['feed_chuong'])){
	
		$val=$_GET['id'];
		$db=new db;
		
		$db->connect();
		$db->query("select t.id,t.ten,t.thutu,t.index_show,t.ngaythem,t.hot,t.new,t.home,t.source as noidung from chapterk2 as t  where  t.id=".intval($val));
		if($db->num_rows()>0){	
			$dt=$db->fetchOne();	
			
		}else{ 
			$dt['error']='Không Tìm thấy chương';
		}
		
		$db->close();
		
		echo json_encode($dt);
		exit();


}


if(isset($_POST['chuong_update'])){

	$md=new Model;
	//Kiem tra chuong co image ko, neu co image thi set image = 1 va curl de download image ve-----
	//Kiem tra thu tu chuong
	//Kiem tra permission, user level 4 thi chi duoc quyen update chuong cua minh, con lai duoc quyen update het
	
	//Kiem tra image có không
	
	

	
	//Kiem tra thutu chuong
	$md->query('select id from chapterk2 where manga='.intval($_POST['truyen']).' and thutu='.floatval($_POST['thutu']).' and id!='.intval($_POST['chuong']));

	
				$md->query('select id from anime where id='.intval($_POST['truyen']));
				
				if($md->num_rows()>0){
					$r=$md->fetchOne();
				}else{
					$data['errors'][]='Manga không tồn tại, xin vui lòng kiểm tra lại!';
					echo json_encode($data);
					exit();
				}
	
	//Kiem tra permission cua user
		$md->query('select id,server,fix,fix_user from chapterk2 where id='.intval($_POST['chuong']));
		if($md->num_rows()>0){
			$r1=$md->fetchOne();
			if($r1['server']!='error'){
				$data['errors'][]='Chương này không nằm trong chương trình fix lỗi, Xin vui lòng kiểm tra lại!';
					echo json_encode($data);
					exit();
			}
		
		}else{
			$data['errors'][]='Chương Không Tồn Tại, Xin vui lòng kiểm tra lại!';
					echo json_encode($data);
					exit();
		}
	//End of kiem tra permission cua user
	
	
	$md->where('id='.intval($_POST['chuong']));
	
	$data=array(
		//'uploader'=>$_SESSION['user']['username'],
			'fix'=>1,
			'fix_user'=>$_SESSION['user']['username'],	
			'source'=>$_POST['noidung'],
			'bk'=>0
	);

	$md->update('chapterk2',$data);
	
	//Clear data trong chuong_content --
	$md->nonquery("update chapterk2 set op=0 where id=".intval($_POST['chuong']));
	$md->nonquery("delete from chapterk_content  where chap_id=".intval($_POST['chuong']));
	echo json_encode(array('message'=>'Fix lỗi chương thành công'));
	
	//Clear cache chuong
	$cache=new cache;
	$cache->flush("anime".intval($_POST['truyen']),'anime');
	$id=intval($_POST['chuong']);
	$sub=$id%100;
	$cache->flush('manga_get_chapter_detail'.$id,'chapter/'.$sub);
	
	exit();
	
}
//End update chuong fix loi -------

